﻿using System;
using System.Collections.Generic;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using ViewContext = Microsoft.AspNetCore.Mvc.Rendering.ViewContext;

namespace Kugar.UI.Web.AdminLTE.PageControls
{
    public abstract class AdminLTEElementBase
    {
        private ViewContext _viewContext = null;
        internal bool _withOuterBorder = false;
        internal string _outerBorderCss = "";

        private Lazy<TagAttrbute> _attr = new Lazy<TagAttrbute>(attrLazy);

        internal AdminLTEElementBase(ViewContext viewContext)
        {
            _viewContext = viewContext;
        }

        public TagAttrbute Attributes => _attr.Value;
        
        public ViewContext ViewContext => _viewContext;

        private static TagAttrbute attrLazy()
        {
            return new TagAttrbute();
        }


        public Dictionary<string, object> HttpTemplateData
        {
            get
            {
                
                if (_viewContext.HttpContext.Items.TryGetValue("__adminltetemplateData",out var dic))
                {
                    return (Dictionary<string, object>)dic;
                }
                else
                {
                    var tmp = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase);

                    _viewContext.HttpContext.Items.TryAdd("__adminltetemplateData", dic);

                    return tmp;
                }
            }
        }


        
        protected AdminLTEUI AdminLTEUI => (AdminLTEUI)_viewContext.ViewBag.WebUI;

        protected IHtmlHelper Html => AdminLTEUI.HtmlHelper;

        protected void WriteClearfix()
        {
            ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");
        }

        // User-defined conversion from Digit to double
        public static implicit operator string(AdminLTEElementBase d)
        {
            return "";
        }
    }



    public abstract class ContainerBase : AdminLTEElementBase,IDisposable
    {
        

        protected ContainerBase(ViewContext viewContext) : base(viewContext)
        {
        }

        public abstract void Dispose();

        internal abstract ContainerBase RenderInternal();

        public override string ToString()
        {
            return "请调用Render函数输出容器";
            return base.ToString();
        }

        public static ContainerBase operator --(ContainerBase container)
        {
            container.RenderAndDispose();

            return container;
        }
    }

    public abstract class ControlBase : AdminLTEElementBase
    {
        public abstract HtmlString Render();

        protected ControlBase(ViewContext viewContext) : base(viewContext)
        {
        }

        public override string ToString()
        {
            return this.Render().ToString();
        }
    }



}
